//
//  HotViewcontroller.m
//  YSBEmployee
//
//  Created by developer on 2021/11/30.
//  Copyright © 2021 ysb. All rights reserved.
//

#import "LHChatBarView.h"
#import "YSBJobChatWebSocketManager.h"

#import "YSBAboutYSBController.h"


#import "YSBPunchCardTableViewCell.h"

#import "YSBWithdrawalMainViewController.h"


#import "HttpClient.h"


@implementation HttpClient

#pragma mark - 修改升级功能,去除请求苹果官网
- (void)updateMethodWithStrive:(BOOL)needStrive andUpdateContent:(NSArray *)updateContentArray andUpdateModel:(UpdateModel *)model {
    
    //获取服务器上应用的最新版本号
    NSString *updateVersion = [YSBCommonTool isBlankString:model.version_no] ? @"" : model.version_no;
    
    NSDictionary *info= [[NSBundle mainBundle] infoDictionary];
    NSString *bundleVersion = [YSBCommonTool isBlankString:info[@"CFBundleShortVersionString"]] ? @"" : info[@"CFBundleShortVersionString"];
    
    NSString *messageStr = [NSString stringWithFormat:@"发现新版本(%@),是否更新?", updateVersion];
    if (updateContentArray.count > 0) {
        NSString *updateStr = @"";
        if (updateContentArray.count > 1) {
            updateStr = [updateContentArray componentsJoinedByString:@" \n"];
        }else{
            updateStr = updateContentArray[0];
        }
        messageStr = updateStr;
    }
    
    if ([updateVersion isEqualToString:bundleVersion]) {
        //            GSToastInCenter(@"当前版本为最新版本无需更新！");
    }
    else {
        if (self.updateAlert == nil) {
            self.updateAlert = [SELUpdateAlert showUpdateAlertWithVersion:updateVersion Description:messageStr isForcingUpdate:needStrive];
            self.updateAlert.hidden = YES;
            //添加弹框界面
            if (self.updateAlert) {
                self.updateAlert.tag = 1;
                [[YSBMSAlertManager shareManager] addAlert:self.updateAlert];
            }
        }
    }
    
}


@end


@implementation YSBWithdrawalMainViewController

#pragma mark - 提现去获取手机验证码确认后再提现
- (void)confirmWithdraw {
    
    if (self.amountView.textField.text.length > 0) { //先请求一次提现接口用来发送提现验证码,只需要withdrawType和money两个字段即可
        NSString *withdrawMoney = self.amountView.textField.text;
        NSString *fee = self.chargeView.model.withdraw_fee;//提现服务费
        
        NSNumber *number = @(withdrawMoney.floatValue - fee.floatValue);
        NSString *realMoney = [NSString stringWithFormat:@"%@", number];
                
        //保存提现方式,方便下次使用
        NSUserDefaults *localSave = [NSUserDefaults standardUserDefaults];
        [localSave setInteger:self.withdrawType forKey:@"withdrawTypeKey"];
        [localSave synchronize];//数据同步,写入文件
        
        if (self.withdrawType == 7) {//余时保微信科技的提现方式默认也是微信提现
            self.withdrawType = 3;
        }
        
        if ([YSBCommonTool isBlankString:self.model.userInfo.mobile]) {
            return;
        }
        
        NSDictionary *parameter = @{
            @"mobile":self.model.userInfo.mobile,
            @"type":@"withdraw"};
        [[HttpClient sharedInstance] postURL:@"https://common.ysbjob.com/v1/user/code" parameter:parameter success:^(id responseObject) {
            
            NSNumber *code = responseObject[@"status_code"];
            if ([code intValue] == 200) {
                YSBRegisterViewController *vc = [[YSBRegisterViewController alloc] initWithRegisterPhone:self.model.userInfo.mobile andMoney:realMoney andWithdrawType:self.withdrawType andIsRegister:4 andWxChannel:self.wxChannel jumpType:1];
                [self.navigationController pushViewController:vc animated:YES];
            }
            
        } failure:^(NSError *error) {
            
        }];
        

    }
    
}


@end



@implementation YSBPunchCardTableViewCell


#pragma mark - 固定时间高度计算
+ (CGFloat)heightForCellWithWorkingTimeModel:(YSBWorkingTimeModel *)workingTimeModel andPunchItemModel:(YSBPunchItemModel *)punchItemModel andPunchCardInfoModel:(YSBPunchCardInfoModel *)model {
    
    if (workingTimeModel.isShowStartPunchBtnView && punchItemModel.startOrEndType == 0) {//处理未到上班打卡时间显示按钮的逻辑
        return 260;
    }
    if (workingTimeModel.isShowEndPunchBtnView && punchItemModel.startOrEndType == 1) {//处理未到下班打卡时间显示按钮的逻辑
        return 260;
    }
    
    PunchStatus status = [YSBPunchCardTableViewCell getPunchStatusWithPunchItemModel:punchItemModel andWorkingTimeModel:workingTimeModel andPunchCardInfoModel:model];

    //0:未打卡 1：等待打卡 2：迟到 3：早退 4：正在进行 5：正常已打卡
    switch (status) {
        case 0:
            return 30;
            break;
        case 1:{
            if (workingTimeModel.ID.integerValue == model.puchInfoModel.timeInfoModel.ID.integerValue) {
                if (punchItemModel.punch_time.length > 0) {
                    return 160;
                }
                
                if (punchItemModel.type.integerValue % 2 == 0 && model.puchInfoModel.punch_type % 2 == 0) {//上班卡
                    return 260;
                }
                if (punchItemModel.type.integerValue % 2 == 1 && model.puchInfoModel.punch_type % 2 == 1) {//下班卡
                    return 260;
                }
                
                if (model.puchInfoModel.punch_type == 2) {
                    return  30;
                }
                if (model.puchInfoModel.punch_type == 0 && punchItemModel.type.integerValue % 2 == 1) {//1下班
                    return 30;
                }
                if (model.puchInfoModel.punch_type == 1 && punchItemModel.type.integerValue % 2 == 0) {//上班
                    return 30;
                }
                return 160;
            }
            
            if ([YSBCommonTool isBlankString:model.puchInfoModel.timeInfoModel.start_time]) {//没有打卡信息的等待上班
                if (punchItemModel.punch_time.length > 0) {//有打卡记录时间
                    return 160;
                }
                return 30;
            }
            
            if (punchItemModel.punch_time.length > 0) {//有打卡记录时间
                return 160;
            }
            else{
                return 30;
            }
        }
            break;
        case 2:
            return 160;
            break;
        case 3:
            return 160;
            break;
        case 4:
            return 260;
            break;
        case 5:
            return 160;
            break;
        default:{
            return 160;
        }
            break;
    }

}


@end



@implementation YSBAboutYSBController

#pragma mark - 注销账号事件
- (void)cancellationAction {
    YSBCancellationViewController *vc = [[YSBCancellationViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}


@end



@implementation LHChatBarView

- (void)setupSubViews {
    
    if (self.isJobChat == NO) {
        [self addSubview:self.onlineBtn];
    }
    
    [self addSubview:self.textView];
    
    if (self.isJobChat == NO) {
        [self addSubview:self.moreBtn];
    }
    
    [self addSubview:self.placeHolderLabel];
    self.placeHolderLabel.hidden = self.textView.text.length > 0;
    
}

@end


@implementation YSBJobChatWebSocketManager


//处理相应执行方法
- (void)msgToGoWithDict:(NSDictionary *)dict {
    
    if (![YSBCommonTool isBlankString:dict[@"status"]]) {//获取状态
        
        NSString *status = dict[@"status"];
        
        if ([status isEqualToString:@"bind_success"]) {
            NSLog(@"求职聊天绑定成功222");
        }
        else if ([status isEqualToString:@"bind_fail"]) {
            NSLog(@"求职聊天绑定失败222");
        }
        else if ([status isEqualToString:@"send_forbid"]) {
            NSLog(@"求职聊天禁止发送222");
            
            if ([dict[@"content"] isKindOfClass:[NSDictionary class]]) {
                NSDictionary *contentDict = dict[@"content"];
                NSString *msg_type = contentDict[@"msg_type"];
                NSString *ID = contentDict[@"from"];
                NSString *msg = contentDict[@"msg"];
                
                if ([msg_type isEqualToString:@"reply"]) {//沟通回复
                    if (![YSBCommonTool isBlankString:contentDict[@"from"]]) {
                        [self saveReplyModelWithIsReply:YES andEmployerID:ID];
                    }
                }
                //收到文本的求职聊天消息
                if ([msg_type isEqualToString:@"text"]) {
                    NSLog(@"求职聊天进入text");
                    [self saveTextMsgForbidWithDict:contentDict];
                    [[YSBJobChatWebSocketManager shared] saveReplyModelWithIsSendForbid:YES andEmployerID:ID andMsg:msg];
                }
                //收到求职聊天的招聘邀请消息
                if ([msg_type isEqualToString:@"invite"]) {
                    NSLog(@"进入了邀请");
                    [self saveInvitationForbidWithDict:contentDict];
                    
                    [[YSBJobChatWebSocketManager shared] saveReplyModelWithIsSendForbid:YES andEmployerID:ID andMsg:msg];
                }
            }
            
            [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshJobChatRoomMessageNotification" object:dict];
            
        }
        else if ([status isEqualToString:@"success"]) {
            NSLog(@"求职聊天系统心跳Ping返回成功");
        }
        else if ([status isEqualToString:@"system_success"] ||
                 [status isEqualToString:@"send_un_forbid"] ) {
            NSLog(@"求职聊天系统返回消息成功");
            
            if ([dict[@"content"] isKindOfClass:[NSString class]]) {//判断是否字符串
                YSBToastInCenter(dict[@"content"]);
            }
            
            if ([dict[@"content"] isKindOfClass:[NSDictionary class]]) {//判断是否字典
                NSLog(@"求职聊天进入content");

                NSDictionary *contentDict = dict[@"content"];
                if (![YSBCommonTool isBlankString:contentDict[@"msg_type"]]) {
                    NSLog(@"求职聊天进入msg_type");
                    //msg_type : text文本消息  invite招聘邀请  reply沟通确认 chat_list聊天列表 grey 禁止聊天
                    NSString *msg_type = contentDict[@"msg_type"];
                    
                    //1.求职消息聊天列表
                    if ([msg_type isEqualToString:@"chat_list"]) {
                        NSLog(@"求职聊天进入chat_list");
                        
                        YSBJobChatListModel *jobChatListModel = [YSBJobChatListModel yy_modelWithJSON:contentDict];
                        
                        NSLog(@"求职聊天进入chat_list,msg_type = %@",jobChatListModel.msg_type);
                        [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshJobChatMessageNotification" object:jobChatListModel];
                        
                        //本地保存
                        [self saveJobChatListWithModel:jobChatListModel];
                        
                        if (jobChatListModel.all_un_read_number > 0) {
                            //设置底部图标未读求职消息数
                            [self showTabBarItemNum];
                        }
                        
                        
                    }
                    
                    //2.收到单个用户在求职聊天消息
                    if ([msg_type isEqualToString:@"text"]) {
                        NSLog(@"求职聊天进入text");
                        [self saveTextMsgWithDict:contentDict];
                        
                    }
                    
                    //3.收到单个用户在求职聊天的招聘邀请消息
                    if ([msg_type isEqualToString:@"invite"]) {
                        NSLog(@"进入了邀请");
                        [self saveInvitationMsgWithDict:contentDict];
                    }
                    
                    
                    //4.收到单个用户在求职聊天的招聘邀请回复消息
                    if ([msg_type isEqualToString:@"invite_res"]) {
                        NSLog(@"进入了邀请回复");
                        YSBJobChatModel *jobChatModel = [YSBJobChatModel yy_modelWithJSON:contentDict];
                        [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshJobChatRoomMessageNotification" object:jobChatModel];
                        
                        //解开禁言
                        NSString *ID = jobChatModel.from == 0 ? @"" : [NSString stringWithFormat:@"%ld",jobChatModel.from];
                        [self saveReplyModelWithIsSendForbid:NO andEmployerID:ID andMsg:@""];
                        
                        if (![[YSBCommonTool getCurrentVC] isKindOfClass:[YSBJobChatVC class]]) {//未在聊天界面需要保存到历史消息
                            [self replaceInvitationModelWithModel:jobChatModel];
                            NSLog(@"进入未在聊天界面的离线邀请反馈");
                        }
                    }
                }
            }
        }
    }
}


#pragma mark - 存放文本但不解禁
- (void)saveTextMsgForbidWithDict:(NSDictionary *)contentDict {
    
    YSBJobChatModel *jobChatModel = [YSBJobChatModel yy_modelWithJSON:contentDict];
    
    NSString *ID = jobChatModel.from == 0 ? @"" : [NSString stringWithFormat:@"%ld",jobChatModel.from];

    if (![[YSBCommonTool getCurrentVC] isKindOfClass:[YSBJobChatVC class]]) {//未在聊天界面需要保存到历史消息
        LHMessageModel *messageModel = [self textModelConversionWithModel:jobChatModel];
        NSArray *historyArr = [self catchHistoryChatRecordWithEmployerID:ID];
        
        NSMutableArray *dataSource = [[NSMutableArray alloc] initWithArray:historyArr];
        [dataSource addObject:messageModel];
        [self saveHistoryChatRecordWithEmployerID:ID andDataArray:[dataSource copy]];
    }
    
}


#pragma mark - 存放邀请消息但不解封
- (void)saveInvitationForbidWithDict:(NSDictionary *)content {
    
    YSBInvitationChatModel *iModel = [YSBInvitationChatModel yy_modelWithJSON:content];

    NSString *ID = iModel.from == 0 ? @"" : [NSString stringWithFormat:@"%ld",iModel.from];
    
    if (![[YSBCommonTool getCurrentVC] isKindOfClass:[YSBJobChatVC class]]) {//未在聊天界面需要保存到历史消息
        LHMessageModel *messageModel = [self invitationModelConversionWithModel:iModel];
        NSArray *historyArr = [self catchHistoryChatRecordWithEmployerID:ID];
        
        NSMutableArray *dataSource = [[NSMutableArray alloc] initWithArray:historyArr];
        [dataSource addObject:messageModel];
        [self saveHistoryChatRecordWithEmployerID:ID andDataArray:[dataSource copy]];
    }
    
}


@end




